home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d20 / listecho.arc / LISTECHO.C next >
C/C++ Source or Header  |  1990-03-10  |  14KB  |  394 lines

  1. #include    <dir.h>
  2. #include    <dos.h>
  3. #include    <process.h>
  4. #include    <stdio.h>
  5. #include    <stdlib.h>
  6. #include    <string.h>
  7.  
  8. #include    "listecho.h"
  9.  
  10. #define     MAXNODES    4096
  11. #define     MAXSTRING   256
  12. #define     MAXWIDTH    50
  13.  
  14. struct singlenode   seenbylist[MAXNODES];
  15. struct doublenode   pathlist[MAXNODES];
  16. struct singlenode   prevnode;
  17. int                 maxseenby;
  18. int                 maxpath;
  19. int                 points_ok;
  20. int                 smallpaths;
  21.  
  22. int compsinglenode(struct singlenode *elem1, struct singlenode *elem2) {
  23.     if (elem1->net > elem2->net) return(1);
  24.     if (elem1->net < elem2->net) return(-1);
  25.     if (elem1->node > elem2->node) return(1);
  26.     if (elem1->node < elem2->node) return(-1);
  27.     return(0);
  28. }
  29.  
  30. int compdoublenode(struct doublenode *elem1, struct doublenode *elem2) {
  31.     if (elem1->fromnet > elem2->fromnet) return(1);
  32.     if (elem1->fromnet < elem2->fromnet) return(-1);
  33.     if (elem1->fromnode > elem2->fromnode) return(1);
  34.     if (elem1->fromnode < elem2->fromnode) return(-1);
  35.     if (elem1->tonet > elem2->tonet) return(1);
  36.     if (elem1->tonet < elem2->tonet) return(-1);
  37.     if (elem1->tonode > elem2->tonode) return(1);
  38.     if (elem1->tonode < elem2->tonode) return(-1);
  39.     return(0);
  40. }
  41.  
  42. void parse_seenby(char *line) {
  43.     char            *p;
  44.     char            myline[MAXSTRING];
  45.     char            *token;
  46.     int             mark;
  47.     unsigned int    net;
  48.     unsigned int    node;
  49.     unsigned int    prevnet;
  50.     int                i;
  51.  
  52.     strcpy(myline,line);
  53.     p = strstr(myline,"SEEN-BY:");
  54.     p += 8;
  55.     token = strtok(p," ");
  56.     while (token) {
  57.         if (strstr(token,"/")) {
  58.             if (sscanf(token,"%u/%u",&net,&node) == 2)
  59.                 prevnet = net;
  60.         }
  61.         else {
  62.             sscanf(token,"%u",&node);
  63.             net = prevnet;
  64.         }
  65.  
  66.         if ((points_ok) || (net < 1000)) {
  67.             if (maxseenby == -1) {
  68.                 seenbylist[0].net = net;
  69.                 seenbylist[0].node = node;
  70.                 maxseenby = 1;
  71.             }
  72.             else {
  73.                 mark = 0;
  74.                 for (i=0;i<maxseenby;i++) {
  75.                     if (seenbylist[i].net == net)
  76.                         if (seenbylist[i].node == node) {
  77.                             mark = -1;
  78.                             break;
  79.                         }
  80.                 }
  81.                 if (!mark) {
  82.                     seenbylist[maxseenby].net = net;
  83.                     seenbylist[maxseenby].node = node;
  84.                     maxseenby++;
  85.                     if (maxseenby == MAXNODES) {
  86.                         puts("Seen-by list too large!!!");
  87.                         exit(1);
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.         token = strtok(NULL," ");
  93.     }
  94. }
  95.  
  96. void parse_path(char *line) {
  97.     char                myline[MAXSTRING];
  98.     struct singlenode   thisnode;
  99.     struct singlenode    node1,
  100.                         node2;
  101.     char                *p;
  102.     char                *token;
  103.     unsigned int        currentnet;
  104.     int                    mark;
  105.     int                    i;
  106.  
  107.     currentnet = 0;
  108.     strcpy(myline,line);
  109.     p = strstr(myline,"PATH:");
  110.     p += 5;
  111.     token = strtok(p," ");
  112.     while (token) {
  113.         if (strstr(token,"/")) {
  114.             if (sscanf(token,"%u/%u",&thisnode.net,&thisnode.node) == 2)
  115.                 currentnet = thisnode.net;
  116.         }
  117.         else {
  118.             sscanf(token,"%u",&thisnode.node);
  119.             thisnode.net = currentnet;
  120.         }
  121.         if (prevnode.net != 0) {
  122.             if (compsinglenode(&prevnode,&thisnode)) {
  123.                 if (((!smallpaths) || (prevnode.net != thisnode.net)) &&
  124.                 ((points_ok) || ((thisnode.net<1000) && (prevnode.net<1000)))) {
  125.                     if (maxpath == -1) {
  126.                         if (compsinglenode(&prevnode,&thisnode) == 1) {
  127.                             pathlist[0].fromnet = thisnode.net;
  128.                             pathlist[0].fromnode = thisnode.node;
  129.                             pathlist[0].tonet = prevnode.net;
  130.                             pathlist[0].tonode = prevnode.node;
  131.                         }
  132.                         else {
  133.                             pathlist[0].fromnet = prevnode.net;
  134.                             pathlist[0].fromnode = prevnode.node;
  135.                             pathlist[0].tonet = thisnode.net;
  136.                             pathlist[0].tonode = thisnode.node;
  137.                         }
  138.                         maxpath = 1;
  139.                     }
  140.                     else {
  141.                         if (compsinglenode(&prevnode,&thisnode) == 1) {
  142.                             node1.net = thisnode.net;
  143.                             node1.node = thisnode.node;
  144.                             node2.net = prevnode.net;
  145.                             node2.node = prevnode.node;
  146.                         }
  147.                         else {
  148.                             node1.net = prevnode.net;
  149.                             node1.node = prevnode.node;
  150.                             node2.net = thisnode.net;
  151.                             node2.node = thisnode.node;
  152.                         }
  153.  
  154.                         mark = 0;
  155.                         for (i=0;i<maxpath;i++) {
  156.                             if (pathlist[i].fromnet == node1.net)
  157.                                 if (pathlist[i].fromnode == node1.node)
  158.                                     if (pathlist[i].tonet == node2.net)
  159.                                         if (pathlist[i].tonode == node2.node) {
  160.                                             mark = -1;
  161.                                             break;
  162.                                         }
  163.                         }
  164.                         if (!mark) {
  165.                             pathlist[maxpath].fromnet = node1.net;
  166.                             pathlist[maxpath].fromnode = node1.node;
  167.                             pathlist[maxpath].tonet = node2.net;
  168.                             pathlist[maxpath].tonode = node2.node;
  169.                             maxpath++;
  170.                             if (maxpath == MAXNODES) {
  171.                                 puts("Paths too large!!!");
  172.                                 exit(1);
  173.                             }
  174.                         }
  175.                     }
  176.                 }
  177.             }
  178.         }
  179.         prevnode.net = thisnode.net;
  180.         prevnode.node = thisnode.node;
  181.         token = strtok(NULL," ");
  182.     }
  183. }
  184.  
  185. void main(int argc, char *argv[]) {
  186.     char                directory[MAXSTRING];
  187.     char                path[MAXSTRING];
  188.     char                msgpath[MAXSTRING];
  189.     char                printline[MAXSTRING];
  190.     char                addstring[MAXSTRING];
  191.     FILE                *fp;
  192.     FILE                *outfile;
  193.     struct ffblk        myffblk;
  194.     int                 i;
  195.     unsigned int        prevnet;
  196.     char                option;
  197.     char                *message;
  198.     char                *p,
  199.                         *q;
  200.     int                 index;
  201.     int                 firstprint;
  202.  
  203.     puts("Echo Lister (C)opywrong 1989 Ronald Bruintjes - The Sorcerer's Cave 2:281/600");
  204.     if ((argc != 2) && (argc != 3)) {
  205.         puts("Usage: listecho <directory> <option>");
  206.         puts("Makes a list of all nodes listed in the SEEN-BY's of that area");
  207.         puts("and a list of all nodes linked by the PATH lines.");
  208.         puts("Option: -v    Verbose Pathlinks (all links possible)");
  209.         puts("        -s    Small Pathlinks (only between different nets)");
  210.         puts("        -t    Tiny Pathlinks (Small, and leave out nets >1000)");
  211.         puts("              This is the default.");
  212.         exit(0);
  213.     }
  214.  
  215.     maxseenby = -1;
  216.     maxpath = -1;
  217.     points_ok = 0;
  218.     smallpaths = -1;
  219.  
  220.     strcpy(directory,argv[1]);
  221.     strcpy(path,directory);
  222.     strcat(path,"*.msg");
  223.  
  224.     option = argv[2][1];
  225.     switch (option) {
  226.         case 's':
  227.             smallpaths = -1;
  228.             points_ok = -1;
  229.             break;
  230.         case 't':
  231.             smallpaths = -1;
  232.             points_ok = 0;
  233.             break;
  234.         case 'v':
  235.             smallpaths = 0;
  236.             points_ok = -1;
  237.             break;
  238.         default:
  239.             smallpaths = -1;
  240.             points_ok = 0;
  241.     }
  242.  
  243.     if (option == 's') smallpaths = -1;
  244.     if (option == 't') {
  245.         smallpaths = -1;
  246.         points_ok = 0;
  247.     }
  248.  
  249.     if (findfirst(path,&myffblk,0)) {
  250.         puts("No messages in that directory");
  251.         exit(1);
  252.     }
  253.  
  254.     do {
  255.         prevnode.net = 0;
  256.         prevnode.node = 0;
  257.  
  258.         strcpy(msgpath,directory);
  259.         strcat(msgpath,myffblk.ff_name);
  260.         puts(msgpath);
  261.         fp = fopen(msgpath,"rb");
  262.         if (fp == NULL) {
  263.             printf("Can't open '%s'.\n",msgpath);
  264.         }
  265.         else {
  266.             message = malloc((int)myffblk.ff_fsize + 2);
  267.             if (message == NULL)
  268.                 printf("No memory to load this message! - '%s'.\n",myffblk.ff_name);
  269.             else {
  270.                 if (fread(message,1,(int)myffblk.ff_fsize,fp) != (int) myffblk.ff_fsize)
  271.                     puts("Error reading message");
  272.                 else {
  273.                     message[(int)myffblk.ff_fsize + 1] = 0;
  274.                     index = 190;
  275.                     while (index < myffblk.ff_fsize) {
  276.                         p = strstr(message + index,"SEEN-BY:");
  277.                         if ((p != NULL) && ((p - message) < myffblk.ff_fsize)) {
  278.                             /* SEEN-BY found */
  279.                             q = strchr(p,0x0D);
  280.                             if ((q != NULL) && ((q - message) < myffblk.ff_fsize)) {
  281.                                 /* CR found */
  282.                                 *q = 0;
  283.                                 index = q - message + 1;
  284.                                 parse_seenby(p);
  285.                             }
  286.                         }
  287.                         else {
  288.                             /* No SEEN-BY found */
  289.                             p = strstr(message + index,"PATH:");
  290.                             if ((p != NULL) && ((p - message) < myffblk.ff_fsize)) {
  291.                                 /* PATH found */
  292.                                 q = strchr(p,0x0D);
  293.                                 if ((q != NULL) && ((q - message) < myffblk.ff_fsize)) {
  294.                                     /* CR found */
  295.                                     *q = 0;
  296.                                     parse_path(p);
  297.                                     index = q - message + 1;
  298.                                 }
  299.                             }
  300.                             else {
  301.                                 /*if (p == NULL) puts("No PATH found in msg?");*/
  302.                                 if ((p-message) < myffblk.ff_fsize) puts("PATH found after end of message!");
  303.                                 /* No PATH found */
  304.                                 break;
  305.                             }
  306.                         }
  307.                     }
  308.                 }
  309.                 free(message);
  310.             }
  311.  
  312.             fclose(fp);
  313.         }
  314.     } while (!findnext(&myffblk));
  315.  
  316.     unlink("OUTPUT");
  317.     outfile = fopen("OUTPUT","wt");
  318.     if (outfile == NULL) {
  319.         puts("Error opening output file - aborting.");
  320.         exit(1);
  321.     }
  322.  
  323.     if (maxseenby>0) {
  324.         qsort(seenbylist,maxseenby,sizeof(struct singlenode),compsinglenode);
  325.         puts("\nList of nodes receiving this echo:");
  326.         prevnet = 0;
  327.         *printline = 0;
  328.         firstprint = -1;
  329.         for (i=0; i<maxseenby; i++) {
  330.             if (prevnet != seenbylist[i].net) {
  331.                 sprintf(addstring,"%d/%d ",seenbylist[i].net,seenbylist[i].node);
  332.                 prevnet = seenbylist[i].net;
  333.             }
  334.             else
  335.                 sprintf(addstring,"%d ",seenbylist[i].node);
  336.             strcat(printline,addstring);
  337.             if (strlen(printline)>MAXWIDTH) {
  338.                 if (firstprint) {
  339.                     puts(printline);
  340.                     fprintf(outfile,"%s\n",printline);
  341.                     firstprint = 0;
  342.                 }
  343.                 else {
  344.                     printf("*%s\n",printline);
  345.                     fprintf(outfile,"*%s\n",printline);
  346.                 }
  347.                 prevnet = 0;
  348.                 *printline = 0;
  349.             }
  350.         }
  351.         if (firstprint) {
  352.             puts(printline);
  353.             fprintf(outfile,"%s\n",printline);
  354.         }
  355.         else {
  356.             printf("*%s\n",printline);
  357.             fprintf(outfile,"*%s\n",printline);
  358.         }
  359.     }
  360.  
  361.     if (maxpath>0) {
  362.         qsort(pathlist,maxpath,sizeof(struct doublenode),compdoublenode);
  363.         puts("\nList of paths in this echo:");
  364.         *printline = 0;
  365.         firstprint = -1;
  366.         for (i=0; i<maxpath; i++) {
  367.             sprintf(addstring,"%d/%d-%d/%d ",pathlist[i].fromnet,pathlist[i].fromnode,
  368.                                              pathlist[i].tonet,pathlist[i].tonode);
  369.             strcat(printline,addstring);
  370.             if (strlen(printline)>MAXWIDTH) {
  371.                 if (firstprint) {
  372.                     puts(printline);
  373.                     fprintf(outfile,"%s\n",printline);
  374.                     firstprint = 0;
  375.                 }
  376.                 else {
  377.                     printf("*%s\n",printline);
  378.                     fprintf(outfile,"*%s\n",printline);
  379.                 }
  380.                 prevnet = 0;
  381.                 *printline = 0;
  382.             }
  383.         }
  384.         if (firstprint) {
  385.             puts(printline);
  386.             fprintf(outfile,"%s\n",printline);
  387.         }
  388.         else {
  389.             printf("*%s\n",printline);
  390.             fprintf(outfile,"*%s\n",printline);
  391.         }
  392.     }
  393. }
  394.